#include "../common/icsearch.hpp"
#include <iostream>
using namespace XKL;

int main()
{
    initLogger("stdout", "", 1);
    // elasticlient::Client client({"http://127.0.0.1:9200/"});
    // auto rsp = client.search("user", "_doc", "{\"query\" : {\"match_all\":{} }}");
    std::vector<std::string> arr = {"http://127.0.0.1:9200/"};
    auto client = std::make_shared<elasticlient::Client>(arr);
    // 创建索引
    bool ret = ESIndex(client, "test_user").append("nickname").append("phone", "keyword", "standard", true).create("");
    if(!ret)
    {
        LOG_WARN("创建索引失败！");
        return -1;
    }
    // 新增数据
    ret = ESInsert(client, "test_user").append("nickname", "leo").append("phone", "1234").insert("001");
    if(!ret)
    {
        LOG_WARN("新增数据失败！");
        return -1;
    }
    // 修改数据
    ret = ESInsert(client, "test_user").append("nickname", "leo").append("phone", "5678").insert("001");
    if(!ret)
    {
        LOG_WARN("新增数据失败！");
        return -1;
    }
    // 查询数据
    auto user = ESSearch(client, "test_user").appendShouldMatch("phone.keyword", "5678").appendMustNotTerms("nickname.keyword", {"leo"}).search();
    if(user.empty() || !user.isArray())
    {
        LOG_WARN("查询数据不存在或者数据不为数组");
        return -1;
    }
    for(int i = 0; i < user.size(); i++)
        std::cout << user[i]["_source"]["nickname"] << " " << user[i]["_source"]["phone"] << std::endl;
    // 删除数据
    ret = ESRemove(client, "test_user", "_doc").remove("");
    if(!ret)
    {
        LOG_WARN("删除数据失败");
        return -1;
    }
    return 0;
}